package com.example.administrator.robot_shanlong_app.socket;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.administrator.robot_shanlong_app.constant.Flag;
import com.example.administrator.robot_shanlong_app.dataconvert.ConvertData;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class SocketChart extends Thread {
    //监听与连接
    private Socket socketClient;
    //io流操作
    private InputStream input;
    private OutputStream output;
    private Handler hclient;
    //接收线程
    private Receive_forServerMessage recevice;
    //连接状态
    private boolean isconnect = false;
    //一直运行监听数据
    private boolean isrun = false;
    //IP 和 Port
    private String Address;
    private int Port;

    public SocketChart(Handler hui) {
        this.hclient = hui;
        this.isrun = true;
    }


    @Override
    public void run() {

        try {

            if (Address.equals("") && Port < 0) {

            } else {
                if (isconnect == false)
                    Flash();
                if (socketClient != null) {
                    recevice = new Receive_forServerMessage(hclient, socketClient);
                    recevice.start();
                    input = socketClient.getInputStream();
                    output = socketClient.getOutputStream();
                }
            }

        } catch (Exception ie) {
            ie.printStackTrace();
        } finally {


        }
    }

    //刷新连接
    public void Flash() {
        try {
            isconnect = true;
            socketClient = new Socket(Address, Port);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void SendError(String string) {
        hclient.sendMessage(hclient.obtainMessage(Flag.ERROR_STATE, string));
    }

    //发送消息队列
    public void SenderMessage(byte [] msg) {
        Log.i("SenderMessge", msg.toString());
        try {
            if (output != null) {
                Log.i("SenderMessge", "" + msg);
                output.write(msg);
                output.flush();
                hclient.sendMessage(hclient.obtainMessage(Flag.WRITE_STATE, -1, -1, msg));
                Log.i("Sendermessage", "数据发送成功");
            }
        } catch (IOException ioe) {

        }
    }

    public void SetIpAdress(String ip) {
        this.Address = ip;
    }

    //设置端口
    public void SetPort(int port) {
        this.Port = port;


    }

    //发送错误
    public void SenderError() {
        hclient.sendMessage(hclient.obtainMessage(Flag.ERROR_STATE));
    }

    //关闭数据与连接
    public void Close() {
        try {
            isrun = false;
            isconnect = false;

            if (socketClient != null) {
                socketClient.close();
                this.stop();

            }
            if (input != null) {
                input.close();
            }
            if (output != null) {

            }
            if (recevice != null) {
                recevice.stop();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
